package com.project.pet.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.pet.common.ResponseData;
import com.project.pet.mapper.BannerMapper;
import com.project.pet.pojo.Banner;
import com.project.pet.pojo.History;
import com.project.pet.utils.ResponseDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BannerService {
    @Autowired
    private BannerMapper mapper;

    private List<Banner> findBysort(Integer sort){
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sort",sort);
        List<Banner> bannerList = mapper.selectByExample(example);
        return bannerList;

    }
    public ResponseData<Object> insert(Banner banner){
        if(!findBysort(banner.getSort()).isEmpty())
            return ResponseDataUtil.buildError("存在相同的排序值");
        int res = mapper.insertSelective(banner);
        return res>0?ResponseDataUtil.buildSuccess("操作成功"):ResponseDataUtil.buildError("操作失败");
    }
    public ResponseData<Object> updateByPrimaryKey(Banner banner){
        List<Banner> bannerList = findBysort(banner.getSort());
        if(!bannerList.isEmpty()){
            if(bannerList.get(0).getSort()!=banner.getSort()){
                return ResponseDataUtil.buildError("存在相同的排序值");
            }
        }
        int res = mapper.updateByPrimaryKeySelective(banner);
        return res>0?ResponseDataUtil.buildSuccess("操作成功"):ResponseDataUtil.buildError("操作失败");
    }
    public ResponseData<Object> selectAll(){
        Example example = new Example(Banner.class);
        example.orderBy("sort").asc();
        return ResponseDataUtil.buildSuccess(mapper.selectByExample(example));
    }
    @Transactional
    public ResponseData<Object> delete(List<Integer> ids){
        try{
            for (Integer id : ids) {
                mapper.deleteByPrimaryKey(id);
            }
            return ResponseDataUtil.buildSuccess("操作成功");
        }catch (Exception e){
            return ResponseDataUtil.buildError("操作失败");
        }
    }

    public ResponseData<Object> pagingQuery(HashMap<String,Object> map){
        Integer pageIndex = Integer.parseInt(map.get("pageIndex").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        map.remove("pageIndex");
        map.remove("pageSize");
        Example example = new Example(Banner.class);
        example.orderBy("sort").asc();
        PageHelper.startPage(pageIndex,pageSize);
        List<Banner> list = mapper.selectByExample(example);
        PageInfo<Banner> pageInfo = new PageInfo<>(list);
        return ResponseDataUtil.buildSuccess(pageInfo);
    }
    public ResponseData<Object> selectByMap(HashMap<String,Object> map){
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        example.orderBy("sort").asc();
        for(Map.Entry<String, Object> entry : map.entrySet()){
            String key = entry.getKey();
            Object value = entry.getValue();
            if(!StringUtils.isEmpty(value))
                criteria.andEqualTo(key,value);
        }
        return ResponseDataUtil.buildSuccess(mapper.selectByExample(example));
    }
}
